package com.cxyd.modules.wcs.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wcs.dto.StnMappingDTO;
import com.cxyd.modules.wcs.dto.StockDTO;
import com.cxyd.modules.wcs.dto.request.BatchForbidRequest;
import com.cxyd.modules.wcs.entity.StockEntity;
import com.cxyd.modules.wcs.service.StnMappingService;
import com.cxyd.modules.wcs.service.StockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wcs/stock")
public class StockController {
    @Resource
    private StockService stockService;

    @GetMapping("page")
    @RequiresPermissions("wcs:stock:page")
    public Result<PageData<StockDTO>> page(@RequestParam Map<String, Object> params) {
        final PageData<StockDTO> page = stockService.page(params);
        return new Result<PageData<StockDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wcs:stock:info")
    public Result<StockDTO> get(@PathVariable("id") Long id) {
        StockDTO data = stockService.get(id);

        return new Result<StockDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wcs:stock:save")
    public Result save(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        stockService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wcs:stock:update")
    public Result update(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        stockService.update(dto);

        //如果库存为 0，那么就删掉此条记录
        StockEntity stockEntity = stockService.selectById(dto.getId());
        int stockQty = stockEntity.getStockQty().intValueExact();
        if (stockQty <= 0) {
            stockService.deleteById(stockEntity.getId());
        }

        return new Result();
    }

    @PostMapping("/disables")
    @LogOperation("一键禁用(解禁)")
    public ResponseEntity<?> disable(@RequestBody BatchForbidRequest request) {
        List<Long> ids = request.getIds(); // 获取前端传递的 ID 集合
        Integer status = request.getStatus();
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.badRequest().body("ID 集合不能为空");
        }
        try {
            // 1. 执行业务逻辑（禁用/解禁）
            stockService.stockDisables(ids, status);
            // 2. 返回成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("msg", "操作成功");
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 3. 返回失败响应
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("msg", "操作失败：" + e.getMessage());
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
}
